package tech.kpretty.bean;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Ticket {
    private int ticketNum = 50;
    private final Lock lock = new ReentrantLock();

    public synchronized void sale() {
        if (ticketNum > 0) {
            System.out.println(Thread.currentThread().getName() + "卖出一张票，还剩" + (--ticketNum) + "张");
        }
    }

    public void saleForLock() {
        lock.lock();
        try {
            if (ticketNum > 0) {
                System.out.println(Thread.currentThread().getName() + "卖出一张票，还剩" + (--ticketNum) + "张");
            }
        } finally {
            lock.unlock();
        }
    }
}
